package com.xm.xmapp;

import android.content.Intent;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.UnderlineSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.app.Type;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.customview.CustomLayout;
import com.xm.xmapp.customview.TipDialog;
import com.xm.xmapp.customview.TipDialog.LeftOnclick;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.myinterface.MyDialogButton;
import com.xm.xmapp.request.bean.Get_user_money_infoRequest;
import com.xm.xmapp.request.bean.Get_user_zhimascoreRequest;
import com.xm.xmapp.request.bean.Zhima_AuthorizeRequest;
import com.xm.xmapp.response.bean.Get_user_zhimascoreRsponse;
import com.xm.xmapp.response.bean.ResponseZhimaAuthorize;
import com.xm.xmapp.response.bean.ResponseUserMoney;
import com.xm.xmapp.tools.MyCache;
import com.xm.xmapp.tools.StringTools;
import com.xm.xmapp.wxapi.WXPayEntryActivity;

public class MyWalletActivity extends BaseActivity implements OnClickListener {

	private TextView mChongzhi, mReturnMoney;
	private TextView mTitle, mMoney, mFrozen_money, mCar_frozen_money;
	private ImageView iv_back, iv_right, iv_zhima;
	private ResponseUserMoney bean;
	private CustomLayout cl_pay_points, cl_account_record_list, cl_coupons_list, cl_recharge, cl_electronic_coupons;
	private DisssmissInterFace mDisssmissable;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mywallet);
	}

	@Override
	public void initView() {

		mChongzhi = (TextView) findViewById(R.id.chongzhi);
		mTitle = (TextView) findViewById(R.id.title);
		iv_back = (ImageView) findViewById(R.id.back);
		iv_right = (ImageView) findViewById(R.id.right);
		mMoney = (TextView) findViewById(R.id.tv_money);

		mFrozen_money = (TextView) findViewById(R.id.tv_frozen_money);
		mCar_frozen_money = (TextView) findViewById(R.id.tv_car_frozen_money);
		mReturnMoney = (TextView) findViewById(R.id.tv_returnMoney);

		cl_pay_points = (CustomLayout) findViewById(R.id.cl_pay_points);
		cl_account_record_list = (CustomLayout) findViewById(R.id.cl_account_record_list);
		cl_coupons_list = (CustomLayout) findViewById(R.id.cl_coupons_list);
		cl_recharge = (CustomLayout) findViewById(R.id.cl_recharge);
		cl_electronic_coupons = (CustomLayout) findViewById(R.id.cl_electronic_coupons);
		iv_zhima = (ImageView) findViewById(R.id.iv_zhima);
	}

	@Override
	public void initData() {
		mTitle.setText("我的钱包");
		iv_right.setVisibility(View.GONE);
		SpannableString ss = new SpannableString("押金退款");
		ss.setSpan(new UnderlineSpan(), 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		mReturnMoney.setText(ss);
		mDisssmissable = new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
			}
		};
	}

	/**
	 * 充值成功后，刷新页面 ,退还押金后刷新页面
	 */

	@Override
	public Object dataChang(int flag, Object data) {
		switch (flag) {
		case Type.A_MyWallet.GET_WALLET:
			get_user_money_infoRequest();
			break;

		default:
			break;
		}
		
		return super.dataChang(flag, data);
	}

	private void get_user_money_infoRequest() {

		Get_user_money_infoRequest requestBean = new Get_user_money_infoRequest();
		promptDialog.show();
		GetDataInterFace<String> listener = new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {
				promptDialog.dismiss();
				bean = JSON.parseObject(response, ResponseUserMoney.class);
				mMoney.setText(StringTools.getString(bean.user_money, "0.00") + "");
				mFrozen_money.setText(StringTools.getString(bean.frozen_money, "0.00") + "");
				mCar_frozen_money.setText(StringTools.getString(bean.car_frozen_money, "0.00") + "");
				cl_pay_points.setText2(bean.pay_points);
				if (bean.is_avoid_frozen_money == 1 || bean.frozen_money >= bean.car_cash_pledge) {
					mChongzhi.setText("充值");
				} else {
					mChongzhi.setText("充值(押金)");
				}
				if (bean.frozen_money <= 0 && bean.car_frozen_money <= 0) {
					mReturnMoney.setVisibility(View.GONE);
				} else {
					mReturnMoney.setVisibility(View.VISIBLE);
				}
				if (bean.zhima_is_authorize == 1) {
					iv_zhima.setImageResource(R.mipmap.zhima);
				} else {
					iv_zhima.setImageResource(R.mipmap.zhima_gray_icon);
				}

			}
		};
		/**
		 * 先显示缓存数据
		 */
		String response = MyCache.getMyCache().getCache(requestBean.getCacheString());
		if (response != null && !response.equals("")) {
			listener.onResponse(response);
		}
		MyAppcation.getMyAppcation().getPostData(this, requestBean, listener, mDisssmissable);
	}

	@Override
	public void initEvent() {
		iv_back.setOnClickListener(this);
		mChongzhi.setOnClickListener(this);
		mReturnMoney.setOnClickListener(this);
		cl_account_record_list.setOnClickListener(this);
		cl_recharge.setOnClickListener(this);
		cl_coupons_list.setOnClickListener(this);
		cl_electronic_coupons.setOnClickListener(this);
		iv_zhima.setOnClickListener(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		get_user_money_infoRequest();
	}

	@Override
	public void onClick(View arg0) {

		switch (arg0.getId()) {
		case R.id.cl_electronic_coupons:
			intent = new Intent(MyAppcation.getMyAppcation(), InputDHQActivity.class);
			startActivity(intent);
			break;

		case R.id.iv_zhima:
			promptDialog.show();
			Get_user_zhimascoreRequest requestBean = new Get_user_zhimascoreRequest();
			MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

				@Override
				public void onResponse(String response) {

					Get_user_zhimascoreRsponse bean = JSON.parseObject(response, Get_user_zhimascoreRsponse.class);
					if (bean.zhima_is_authorize == 1) {
						promptDialog.dismiss();
						intent = new Intent(MyAppcation.getMyAppcation(), AuthorizeZhimaIsActivity.class);
						intent.putExtra("bean", bean);
						startActivity(intent);
					} else {
						getZhimaAuthorize();
					}
				}
			}, mDisssmissable);
			break;

		case R.id.tv_returnMoney:
			if (bean == null) {
				get_user_money_infoRequest();
			}
			intent = new Intent(MyAppcation.getMyAppcation(), ReturnMoneyActivity.class);
			intent.putExtra("bean", bean);
			startActivity(intent);
			break;

		case R.id.back:
			finish();
			break;

		case R.id.cl_coupons_list:
			intent = new Intent(MyAppcation.getMyAppcation(), YHQActivity.class);
			startActivity(intent);
			break;

		case R.id.cl_recharge:
			intent = new Intent(MyAppcation.getMyAppcation(), CZKCZActivity.class);
			startActivity(intent);
			break;

		case R.id.cl_account_record_list:
			intent = new Intent(MyAppcation.getMyAppcation(), AccountRecordListActivity.class);
			startActivity(intent);

			break;
		case R.id.chongzhi:
			if (bean != null) {
				if (bean.zhima_is_authorize == 1) {
					startIntent(true);
				} else {
					if (bean.is_avoid_frozen_money == 1 || bean.frozen_money >= bean.car_cash_pledge) {
						startIntent(false);
					} else {
						promptDialog.show();
						GetDataInterFace<String> listener = new GetDataInterFace<String>() {
							@Override
							public void onResponse(String response) {
								promptDialog.dismiss();
								Get_user_zhimascoreRsponse base = JSON.parseObject(response,
										Get_user_zhimascoreRsponse.class);
								if (base.zhima_is_authorize == 1) {
									startIntent(true);
								} else {
									TipDialog tipDialog = new TipDialog(MyWalletActivity.this, "温馨提示",
											"认证芝麻信用可以减少押金哦!", new MyDialogButton() {

												@Override
												public void setTitle(int flag, String str) {
													getZhimaAuthorize();
												}
											});
									tipDialog.setRightText("去授权");
									tipDialog.setLeftOnclick(new LeftOnclick() {

										@Override
										public void leftOnclick() {
											startIntent(false);
										}
									});
									tipDialog.show();
								}
							}
						};
						Get_user_zhimascoreRequest get_user_zhimascoreRequest = new Get_user_zhimascoreRequest();
						MyAppcation.getMyAppcation().getPostData(this, get_user_zhimascoreRequest, listener,
								mDisssmissable);
					}
				}

			} else {
				get_user_money_infoRequest();
			}

			break;

		default:
			break;
		}
	}

	/**
	 * 跳转芝麻认证
	 */
	protected void getZhimaAuthorize() {
		Zhima_AuthorizeRequest requestBean = new Zhima_AuthorizeRequest();
		MyAppcation.getMyAppcation().getPostData(MyWalletActivity.this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {
				promptDialog.dismiss();
				ResponseZhimaAuthorize bean = JSON.parseObject(response, ResponseZhimaAuthorize.class);
				intent = new Intent(MyAppcation.getMyAppcation(), ShowURLActivity.class);
				intent.putExtra("url", bean.authorize_url);
				intent.putExtra("title", "芝麻信用");
				startActivity(intent);
			}
		}, mDisssmissable);
	}

	public void startIntent(boolean flag) {

		if (bean.is_avoid_frozen_money == 1 || bean.frozen_money >= bean.car_cash_pledge) {
			WXPayEntryActivity.currentStatus = WXPayEntryActivity.WALLET;
			intent = new Intent(MyAppcation.getMyAppcation(), PayActivity.class);
			intent.putExtra("isAuthorize", flag);
			startActivity(intent);
		} else {
			WXPayEntryActivity.currentStatus = WXPayEntryActivity.WZ;
			intent = new Intent(MyAppcation.getMyAppcation(), WZYJActivity.class);
			intent.putExtra("bean", bean);
			intent.putExtra("isAuthorize", flag);
			startActivity(intent);
		}

	}
}
